<?php
header('content-type:text/html;charset=utf8');
//ini_set("display_errors", "On");
//error_reporting(E_ALL | E_STRICT);
require_once '../tools/tools.common.php';
require_once '../system.UIShujuZhongxin.php';
require_once '../bs/bs.publicnumber.php';

Logger::getInstance()->logInfo("=============page_start=============".date("Y-m-d H:i:s"));
function f重新获取openID($paramArray) {
    $公众号appid = Tools::fh通过公众号id获取公众号appid(G_SERV_ID);
    logger()->logDebug2("公众号appid");
    logger()->logDebug2($公众号appid);
    $weixinurl = Tools::GetTransmitUrl($paramArray,$公众号appid,G_SERV_ID);
    logger()->getInstance()->logInfo("weixinurl:".$weixinurl);
    header('location:'.$weixinurl);
    exit;
}

define("版本","20160422");
//@ $code=$_REQUEST['code'];
 $state=null;
 if(array_key_exists('state',$_REQUEST)) $state=$_REQUEST['state'];
if(@$_REQUEST['env']=='21901'){
    $_SESSION['vo用户数据'] = null;
    $openId=$_REQUEST['openId'];
    $_SESSION['vo用户数据']['openID']=$_REQUEST['openId'];
    $bm=new CUI数据中心(Page::y用户中心,$openId);
    $bm->fg给session赋值();
}else{
    $openId=$_SESSION['vo用户数据']['openID'];
}

//echo $openId;
logger()->logDebug2($openId,'index page:');

global $paramArray ;
  $paramArray=false;
$page = null;
if($state){
    //某些页面要传多个参数时，需要通过state参数分解
    if(substr($state,0,3)=='000'){
        $enc=substr($state,3);

        $paramArray=Tools::object2array(json_decode(urldecode($enc)));

        $page = $paramArray[0];
    }else{
        $page=$state;
    }

    //没有session就重新获取
    if(!$_SESSION['vo用户数据']['openID']) {
        f重新获取openID($paramArray);
        exit;
    }

    //session有公众号信息就判断是否是同一个公众号
    if($_SESSION['vo用户数据']['公众号openID']) {
        $图文所属公众号openID = C公众号::ff根据公众号id查询公众号openID(G_SERV_ID);
        if($_SESSION['vo用户数据']['公众号openID'] != $图文所属公众号openID) {
            session_destroy();
            f重新获取openID($paramArray);
            exit;
        }
    }

    #被分享的页面
    $beSharePage = array(
        'Seller20meet_beijing', #北京会议 【第七届全国新生儿疾病诊疗进展学术研讨会】
        'Seller20meet5_huiwu',  #上海会议【危重早产儿的救治与管理学习班】
        'Seller17Shipin_LessonsS',
        'Seller17Shipin_Lessons'
    );
    $用户=pdo()->selectColumns("用户",array("id"),array("openID"=>$_SESSION['vo用户数据']['openID']));
    if(!$用户['id'] and !in_array($page, $beSharePage)) {
        echo "<script>alert('请先加入爱贝儿医'); location.replace('http://mp.weixin.qq.com/s?__biz=MzI3NjE2OTU3NA==&mid=403902577&idx=1&sn=153a3ede424f16e8586dc33d5b3fab8a&scene=0&previewkey=eApt5HNBZqOZzDiMbGw5F8wqSljwj2bfCUaCyDofEow%3D#wechat_redirect');</script>";
        die;
    }

    $bm = new CUI数据中心(Page::m卖家中心_账目,$_SESSION['vo用户数据']['openID']);

    //如果session里只有openID信息就重新赋值
    if(!$_SESSION['vo用户数据']['openID']) {
        $bm->fg给session赋值();
        exit;
    }

    //需要更新用户缓存
    $更新信息 = $bm->fp判断当前用户session是否需要更新($_SESSION['vo用户数据']['id']);
    if($更新信息 ==='是') {
        session_destroy();
        f重新获取openID($paramArray);
        exit;
    }
    //如果有session并且是图文信息操作,判断是否是该openID操作
    if(@$paramArray['openID'] && $_SESSION['vo用户数据']) {
        if($_SESSION['vo用户数据']['openID']!=$paramArray['openID']) {
            getStr('无效或非法链接地址');die;
        }
    }
    require $page.'.php';
    Logger::getInstance()->logInfo("=============page_end=============".date("Y-m-d H:i:s"));
    exit(0);
}



function getStr($str)  {
    echo '<div style="width:80%; height:50%; left:10%; top:10%; position:absolute; z-index: 10; background-color:#d0e9c6; display:block;font-size: 80px"><div style=" height:50%; color:#848484; width: 100%; text-align: center; margin-top: 25%">'.$str.'</div><div style="height:50%; width: 100%; text-align: center; margin-top: -15%"><input type="button"  style=" background-color: #5cb85c;border-color: #4cae4c;color: #fff;width: 50%; height: 50%;border-radius: 6px;font-size: 70px;line-height: 1.33;padding: 10px 16px;" onclick="closeWind()" value="关闭"></div></div>';
    $close = <<<EOT
<script>
function closeWind(){

        if(typeof(WeixinJSBridge)=='undefined'){
            window.close();
        }else

            WeixinJSBridge.invoke('closeWindow',{},function(res){

                //alert(res.err_msg);
                return true;
            });
    }
</script>
EOT;
    echo $close;
}
function getCurrentUserOID(){
    //return $_REQUEST['openId'];
      return $_SESSION['vo用户数据']['openID'];
}

function PrintPageHead(){
    echo '    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

    <meta name="format-detection" content="telephone=no, address=no">
    <meta name="apple-mobile-web-app-capable" content="yes" /> <!-- apple devices fullscreen -->
    <meta name="apple-touch-fullscreen" content="yes"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />

    <!-- Bootstrap -->
    <link href="resource/css/bootstrap.min.css" rel="stylesheet">
    <link href="resource/css/common.css" rel="stylesheet">
    ';
}

function curl_get_contents($url,$timeout=3) {
    $curlHandle = curl_init();
    curl_setopt( $curlHandle , CURLOPT_URL, $url );
    curl_setopt( $curlHandle , CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $curlHandle , CURLOPT_TIMEOUT, $timeout );
    $result = curl_exec( $curlHandle );
    curl_close( $curlHandle );
    return $result;
}
